package com.hro.hro_app.dataController;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hro.hro_app.database.DBManager;
import com.hro.hro_app.main.MyApplication;
import com.hro.hro_app.model.Teacher;

public class TeacherController {

	public static ArrayList<Teacher> getTeacherList() {

		DBManager dbManager = null;
		SQLiteDatabase db = null;
		Cursor cursor = null;

		try {

			ArrayList<Teacher> teachers = new ArrayList<Teacher>();

			dbManager = new DBManager(MyApplication.getAppContext());
			db = dbManager.getWritableDatabase();
			cursor = db.rawQuery("SELECT code FROM teacher ORDER BY code DESC", null);

			while (cursor.moveToNext()) {

				String teachCode = cursor.getString(cursor.getColumnIndex("code"));
				Teacher teacher = new Teacher(teachCode);
				teachers.add(teacher);
			}

			return teachers;
		}
		finally {

			if (cursor != null)
				cursor.close();
			if (db != null)
				db.close();
			if (dbManager != null)
				dbManager.close();
		}
	}

	public static void saveTeacherList(ArrayList<Teacher> tList) {

		DBManager dbManager = null;
		SQLiteDatabase db = null;

		try {

			dbManager = new DBManager(MyApplication.getAppContext());
			db = dbManager.getWritableDatabase();

			// Maak Datbase Leeg
			db.delete("teacher", null, null);

			// Voeg de nieuwe teachers aan de database toe.
			ContentValues values = new ContentValues();

			for (int iloop = 0; iloop < tList.size(); iloop++) {

				String teacherCode = tList.get(iloop).getCode();
				values.put("code", teacherCode);

				// Insert into database
				db.insertWithOnConflict("teacher", null, values, SQLiteDatabase.CONFLICT_REPLACE);
			}
		}
		finally {

			if (db != null)
				db.close();
			if (dbManager != null)
				dbManager.close();
		}
	}

	public static void updateTeacherPresence(ArrayList<Teacher> absent) {

		// Lijst met absente teachers
		for (int i = 0; i < absent.size(); i++) {

			Teacher teacher = absent.get(i);
			setTeacherAbsent(teacher, true);
		}
	}

	public static void setTeacherAbsent(Teacher teacher, boolean absent) {

		DBManager dbManager = null;
		SQLiteDatabase db = null;

		try {

			int a = 0;
			if (absent) {
				a = 1;
			}

			dbManager = new DBManager(MyApplication.getAppContext());
			db = dbManager.getWritableDatabase();

			ContentValues args = new ContentValues();
			args.put("absent", a);
			db.update("teacher", args, "code" + " = ?", new String[] { String.valueOf(teacher.getCode()) });
		}
		finally {

			if (db != null)
				db.close();
			if (dbManager != null)
				dbManager.close();
		}
	}

	public static boolean getTeacherAbsent(String code) {

		DBManager dbManager = null;
		SQLiteDatabase db = null;
		Cursor cursor = null;

		try {

			dbManager = new DBManager(MyApplication.getAppContext());
			db = dbManager.getWritableDatabase();
			cursor = db.rawQuery("SELECT absent FROM teacher WHERE code = '" + code + "'", null);

			cursor.moveToFirst();

			int absent = cursor.getInt(cursor.getColumnIndex("absent"));
			if (absent == 1) {
				return true;
			}
			else {
				return false;
			}
		}
		finally {

			if (cursor != null)
				cursor.close();
			if (db != null)
				db.close();
			if (dbManager != null)
				dbManager.close();
		}
	}
}
